package com.eduplatform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eduplatform.common.Result;
import com.eduplatform.dto.PPTDTO;
import com.eduplatform.service.PPTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * PPT课件控制器
 */
@RestController
@RequestMapping("/api/ppts")
public class PPTController {

    @Autowired
    private PPTService pptService;

    /**
     * 分页查询PPT课件列表
     */
    @GetMapping("/list")
    public Result<Page<PPTDTO>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long subjectId,
            @RequestParam(required = false) Integer educationLevel,
            @RequestParam(required = false) Long schoolId) {
        Page<PPTDTO> result = pptService.listPPTs(page, size, keyword, subjectId, educationLevel, schoolId);
        return Result.success(result);
    }

    /**
     * 获取PPT课件详情
     */
    @GetMapping("/detail/{id}")
    public Result<PPTDTO> detail(@PathVariable Long id) {
        PPTDTO ppt = pptService.getPPTById(id);
        if (ppt == null) {
            return Result.error("PPT课件不存在");
        }
        return Result.success(ppt);
    }

    /**
     * 添加PPT课件
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody PPTDTO pptDTO) {
        // 设置当前用户ID (实际项目中应从认证信息中获取)
        // 这里简化处理，假设用户ID为1
        pptDTO.setUserId(1L);

        boolean result = pptService.addPPT(pptDTO);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 更新PPT课件
     */
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody PPTDTO pptDTO) {
        boolean result = pptService.updatePPT(pptDTO);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("更新失败");
        }
    }

    /**
     * 删除PPT课件
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = pptService.deletePPT(id);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 获取热门PPT课件
     */
    @GetMapping("/hot")
    public Result<List<PPTDTO>> hot(@RequestParam(defaultValue = "8") Integer limit) {
        List<PPTDTO> hotPPTs = pptService.getHotPPTs(limit);
        return Result.success(hotPPTs);
    }

    /**
     * 获取最新PPT课件
     */
    @GetMapping("/latest")
    public Result<List<PPTDTO>> latest(@RequestParam(defaultValue = "8") Integer limit) {
        List<PPTDTO> latestPPTs = pptService.getLatestPPTs(limit);
        return Result.success(latestPPTs);
    }

    /**
     * 记录PPT课件查看
     */
    @PostMapping("/view/{id}")
    public Result<Boolean> view(@PathVariable Long id) {
        boolean result = pptService.viewPPT(id);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("记录查看失败");
        }
    }

    /**
     * 记录PPT课件下载
     */
    @PostMapping("/download/{id}")
    public Result<Boolean> download(@PathVariable Long id) {
        boolean result = pptService.downloadPPT(id);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("记录下载失败");
        }
    }
}